/*****jquery 通用函数******/
//表单提交ajax
function myajaxform(myform,myurl,mycfun,mysfun,myefun,mydatatype,mytype){	
	if(!myform){
		alert("表单对象不能为空!");
		return false;
	}
	$(myform).submit(function(){
		if(!myurl || !mysfun){
			alert("请输入url和回调函数!");
			return false;
		}
		if(!mycfun){	 														//提交前函数
			mycfun="";
		}
		if(!myefun){
			myefun="alert('get error!')";
		}
		if(!mydatatype){
			mydatatype='json';
		}
		if(!mytype){
			mytype='POST';
		}
		var options = {                                                   		//ajaxform表单提交设置
				//target:   '#divid2',                                        	//结果显示目标
				url:        myurl,     											//action目标
				type:  		mytype,
				dataType:	mydatatype,											//默认值
				beforeSubmit: function(){										//提交前
					return eval(mycfun);
				},
				success: function(msg){											//表单提交后
					eval(mysfun);                                
				},
				error : function(){
					eval(myefun); 
				}};
		$(this).ajaxSubmit(options);
		return false;  	
	});
};

//ajax提交
function myajax(myurl,mysfun,myefun,mydatatype,mytype){	
	if(!myurl || !mysfun){
		alert("请输入相关参数!");
		return false;
	}
	if(!myefun){
		myefun="alert('get error!')";
	}
	if(!mydatatype){
		mydatatype='json';
	}
	if(!mytype){
		mytype='POST';
	}
	var options = {                                                  		//ajaxform表单提交设置
			//target:   '#divid2',                                       	 //结果显示目标
			url:        myurl,     											//action目标
			type:  		mytype,
			dataType:	mydatatype,											 //默认值
			success: function(msg){											 //表单提交后
				eval(mysfun);                                
			},
			error : function(){
				eval(myefun); 
			}};
	$.ajax(options);
	return false;
};

//使用url传参数时.先加密处理
function ajax_encode (str) {
    str=encodeURIComponent(str);
    if (navigator.product == 'Gecko') str=str.replace("/%0A/g", "%0D%0A"); 
	//In IE, a new line is encoded as rn, while in Mozilla it's n
    return str;
}

//全选
function select_all(name,type){
	$("input[name='"+name+"']").each(function(i){ 
		$(this).attr("checked", type);
	});
}

//复选框全选/不选/反选
function checkAll(form, sel) {
	for (i = 0, n = form.elements.length; i < n; i++) {
		if(form.elements[i].type == "checkbox") {
			if(form.elements[i].checked == true) {
				form.elements[i].checked = (sel == "all" ? true : false);
			} else {
				form.elements[i].checked = (sel == "none" ? false : true);
			}
		}
	}
}

//选上checkbox情况
function count_select(name){
	var id_list='';
	$("input[name='"+name+"']").each(function(i){	
		var type=$(this).attr("checked");
		var value=$(this).val();
		if(type==true){
			id_list = id_list ? id_list+','+value : id_list+value;
		};
	});	
	if(id_list)
	return id_list;
	else
	return false;
}

//显示隐藏指定区域
function show_div(id,show){
	if(show>0){
		$(id).show();	
	}else{
		$(id).hide();	
	}
}

/*
thobji div的id前缀,如div1就是div,1为序号
div 是选上的序号
num 共有几个块 
inclass 选上的class
outclass 是未被选上的class
type 1为隐藏目标框,样式为操作按键的样式,0时为转换目标,样式为目标样式
*/
function select_div(theObj,div,num,inclass,outclass,type) 
{
	var page='';  
	if(type)
	type='1';
	else
	type='0';
	if(div=='')
	div='0';
	if(num > 0)
	{
		for(var j=0;j<num;j++)
		{
			if(div==j)
			{
				if(type)
				{
					document.getElementById(theObj+j).style.display='inline';
					document.getElementById('bt'+theObj+j).className=inclass;
				}else
				{
					document.getElementById(theObj+j).className=inclass;
				}
				//document.getElementById(theObj+j).className=inclass;
			}else
			{
				if(type)
				{
					document.getElementById(theObj+j).style.display="none";
					document.getElementById('bt'+theObj+j).className=outclass;
				}else
				{
					document.getElementById(theObj+j).className=outclass;
				}
				
			}
		}
	} 
}

//控制图片输出大小
function drawimage(ImgD,width,height)
{
	// <img scr="img.jpg" onload="javascript:drawimage(this,'','');" />
	if(width =='')
	width = 160;
	if(height =='')
	height = 120;
	var flag=false;
 	var image=new Image();
 	image.src=ImgD.src;
	if(image.width>0 && image.height>0){
	  	flag=true;
	  	if(image.width/image.height>= width/height){
	   		if(image.width>width){
				ImgD.width=width;
				ImgD.height=(image.height*width)/image.width;
	   		}else{
				ImgD.width=image.width;
				ImgD.height=image.height;
	   		}
	   		ImgD.alt=image.width+"x"+image.height;
		}
  		else{
	   		if(image.height>height){
				ImgD.height=height;
				ImgD.width=(image.width*height)/image.height;
	   		}else{
				ImgD.width=image.width;
				ImgD.height=image.height;
	   		}
	   		ImgD.alt=image.width+"x"+image.height;
	  	}
	}
}

//增加Cookie
function addCookie(name,value,expireHours){
    var cookieString=name+"="+escape(value);
    //判断是否设置过期时间
    if(expireHours>0){
        var date=new Date();
        date.setTime(date.getTime+expireHours*3600*1000);
        cookieString=cookieString+"; expire="+date.toGMTString();
    }
    document.cookie=cookieString;
}
//获取Cookie值
function getCookie(name){
    var strCookie=document.cookie;
    var arrCookie=strCookie.split("; ");
    for(var i=0;i<arrCookie.length;i++){
        var arr=arrCookie[i].split("=");
        if(arr[0]==name){
            return unescape(arr[1]);
        }
    }
    return false;
}
//删除Cookie 
function deleteCookie(name){
    var date=new Date();
    date.setTime(date.getTime()-10000);
    document.cookie=name+"=; expire="+date.toGMTString();
}


//对表单中 textarea 中的空格和换行处理。
function makeToHtml(formName)
{
	var form;
	form = eval(formName);
	for(i=0;i<form.elements.length;i++)
		if(form.elements[i].type=="textarea")
			{
             form.elements[i].value = convertToHtml(form.elements[i].value);
           }
}
function convertToHtml(str)
{
	var re;
	re  = / /g;
	str = str.replace(re,"&nbsp;");
	re  = /\r\n/g;
	str = str.replace(re,"<br>");
	return str;
}

function refresh(){
	document.getElementById("authImg").src = root+'/authImg?now='+ new Date();
}

//jquery 全选或反选
function checked_selectall(id){
	$("."+id).each(function(i){
		if($(this).attr("checked") == true){
			$(this).attr("checked",false);
		}else{
			$(this).attr("checked",true);
		}
	});
}

//jquery 获取选定记录id,id为class名称
function checked_selectallId(id){
	var orderId="";
	$("."+id).each(function(i){
		if($(this).attr("checked") == true){
			orderId += $(this).attr("id") + "|";
		}else{
		}
	});
	return orderId;
}

//脏字过滤

function Filtrate(text,fuck,sign,Fstr){
	var st=fuck.split(sign); 
	for(var i=0;i<st.length;i++){
      strfuck=new RegExp(st[i],"g");  //创建正则表达式
      text = text.replace(strfuck,Fstr); //过滤字符串
	}
    return text;
}

//过滤html标签
function filterHtmlTag(htmlString){
 var reg=/<[^>]*>|<\/[^>]*>/gm;
 var myString=htmlString.replace(reg,"");
 return myString;
}

//手机号码
function checkMBPhone(phone){
	var GSMPhNo = /^(13[4-9])|(159)|(157)|(158)|(150)|(151)|(13[0-3])|(153)(155)|(156)|/; //以134(5、6、7、8、9)或159,158,151,150开头;
	var num11 = /^\d{11}$/; //11位数字;
	if( "" != phone ){
	  if(num11.exec(phone)){
	    if(GSMPhNo.exec(phone)){
	      return true;
	    }else{
	      //alert("对不起，您在本站点输入的手机号码不正确!");
	      return false;
	    }
	  }else{
	  	//alert(phone);
	    //alert("对不起，您在本站点输入的手机号码不正确,请正确输入11位手机号码(数字)!");
	    return false;
	  }
	}else{
	  //alert("对不起，您在本站点输入的手机号码不正确!");
	  return false;
	}
} 